﻿@model Huirui.Cavan.Presentation.ViewModel.SalesModule.IReceiptViewModel
@{
    Layout = null;
}
@using (Html.BeginForm(ActionConst.ADD, ControllerConst.RECEIPT))
{
    <div class="tool_bar">
        <input id="receipt_save" type="submit" style="display: none;" />
        <input id="receipt_reset" type="reset" style="display: none" />
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'"
            onclick="javascript:$('#receipt_save').click()">保存</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-delete'">
            删除</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-undo'"
            onclick="javascript:$('#receipt_reset').click()">重置</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'">
            查询</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-print'">
            打印</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-collapse'"
            onclick="toggleHeard()">折叠/展开</a>
    </div>
    <table class="form_heard">
        <tr>
            <td class="c100">
                <label>
                    收款日期:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.ReceiptDate, new { id = "receiptDate", style = "height:18px", @class = "Wdate", onfocus = "WdatePicker({minDate:'%y-%M-#{%d+1}'})" })
            </td>
            <td class="c100">
                <label>
                    收款单号:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Id, new { id = "receiptCode" })
            </td>
            <td class="c160">
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>
                    客户*:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Customer.Id, new { id = "customerCode" })
            </td>
            <td colspan="2">
                @Html.TextBoxFor(m => m.Item.Customer.Name, new { id = "customerName" })
            </td>
            <td class="c160">
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>
                    账户:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.ToAccount.Id, new { id = "accountCode" })
            </td>
            <td class="c100">
                <label>
                    支付号:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.AccountNumber, new { id = "accountNumber" })
            </td>
            <td class="c160">
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>
                    币别:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Currency, new { id = "currency" })
            </td>
            <td class="c100">
                <label>
                    汇率:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.CurrencyRate, new { id = "currencyRate" })
            </td>
            <td class="c160">
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>
                    收款金额:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Amount, new { id = "amount" })
            </td>
            <td class="c100">
                <label>
                    现金折扣:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Discount, new { id = "discount" })
            </td>
            <td class="c160">
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>
                    备注:</label>
            </td>
            <td colspan="3">
                @Html.TextBoxFor(m => m.Item.Remark, new { id = "remark" })
            </td>
            <td class="c160">
                <a>请选择常用备注</a>
            </td>
        </tr>
    </table>
    @Html.HiddenFor(m => m.ReceiptDetails, new { id = "receiptDetails" })
}
<div style="display:none">
    @using (Html.BeginForm(ActionConst.LIST, ControllerConst.RECEIVABLES))
    { 
        @Html.HiddenFor(m => m.ReceivablesSearch.CustomerCode, new { id = "searchCustomerCode" })
        @Html.TextBoxFor(m => m.ReceivablesSearch.StartDate, new { id = "searchStartDate" })
        @Html.TextBoxFor(m => m.ReceivablesSearch.EndDate, new { id = "searchEndDate" })
        @Html.TextBoxFor(m => m.ReceivablesSearch.ReceivablesCode, new { id = "searchReceivablesCode" })
        <input type="submit" value="查询" />
    }</div>
<div class="tool_bar_grid">
    <a>显示过滤条件</a><span>|</span><input type="button" id="autoAllocate" value="自动分配" />|未核销金额<input
        type="text" id="unCertificateAmount" />|汇兑损益<input type="text" id="currencyLost" />
</div>
<table id="receivableses" title="待结算业务清单">
</table>
<script type="text/javascript">
    $(function ()
    {
        $("form[action^='/receivables/list']").submit(function ()
        {
            $("#searchCustomerCode").val($("#customerCode").val());
            if ($("#searchCustomerCode").val() === undefined || $("#searchCustomerCode").val() == '')
            {
                alert("请选择客户");
                return false;
            }
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result)
                {
                    if (result.total > 0)
                    {
                        $('#receivableses').datagrid({
                            columns: [[
                                { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
                                { field: 'Id', title: '', hidden: true },
                                { field: 'CurrentAmount', title: '核销金额', width: 80, editor: { type: 'numberbox'} },
                                { field: 'CertificateDate', title: '核销日期', width: 60, editor: { type: 'datebox'} },
                                { field: 'ReceivablesType', title: '业务类型', width: 100 },
                                { field: 'CreateDate', title: '业务日期', width: 100 },
                                { field: 'ReceivablesCode', title: '单据编号', width: 80 },
                                { field: 'Currency', title: '币别', width: 80 },
                                { field: 'CurrencyRate', title: '汇率', width: 80 },
                                { field: 'Amount', title: '应收金额', width: 80 },
                                { field: 'LeftAmount', title: '应收余额', width: 80 }
                            ]],
                            data: result
                        });

                        var rows = $('#receivableses').datagrid('getRows');
                        for (var i = 0; i < rows.length; i++)
                        {
                            $('#receivableses').datagrid('beginEdit', i);
                        }
                    }
                    else
                    {
                        $('#receivableses').datagrid('loadData', { total: 0, rows: [] });
                    }
                },
                error: function ()
                {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });

        $("form[action^='/receipt/add']").submit(function ()
        {
            var rows = $('#receivableses').datagrid('getRows');
            receiptDetails = "[";
            for (var i = 0; i < rows.length; i++)
            {
                $('#receivableses').datagrid('endEdit', i);
                if (i == (rows.length - 1))
                {
                    receiptDetails = receiptDetails + "{ \"Receivables\" : { \"Id\" : " + rows[i].Id + "}, \"Amount\" : " + rows[i].CurrentAmount + ", \"CertificateDate\" : \"" + rows[i].CertificateDate + "\" }";
                }
                else
                {
                    receiptDetails = receiptDetails + "{ \"Receivables\" : { \"Id\" : " + rows[i].Id + "}, \"Amount\" : " + rows[i].CurrentAmount + ", \"CertificateDate\" : \"" + rows[i].CertificateDate + "\" }" + ",";
                }
            }
            receiptDetails = receiptDetails + "]";
            $("#receiptDetails").val(receiptDetails);
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data)
                {
                    if (data)
                    {
                        alert(data.Messages[0]);
                    }
                },
                error: function ()
                {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });

        $("#autoAllocate").bind("click", function ()
        {
            if ($("#amount").val() == '')
            {
                return false;
            }
            var rows = $('#receivableses').datagrid('getRows');
            var rowId;
            var allocateAmount;
            var leftAmount = parseFloat($("#amount").val());
            for (var i = 0; i < rows.length; i++)
            {
                rowId = $(".datagrid-btable").find("tr").filter("[datagrid-row-index=" + i + "]").attr("id");
                $("#" + rowId).find("td").each(function ()
                {
                    if ($(this).attr("field") == "Amount")
                    {
                        allocateAmount = parseFloat($(this).find("div").html());
                        if (allocateAmount < 0)
                        {
                            leftAmount += -allocateAmount;
                        }
                    }
                });
            }
            for (var i = 0; i < rows.length; i++)
            {
                $('#receivableses').datagrid('selectRow', i);
                rowId = $(".datagrid-btable").find("tr").filter("[datagrid-row-index=" + i + "]").attr("id");
                $("#" + rowId).find("td").each(function ()
                {
                    if ($(this).attr("field") == "Amount")
                    {
                        allocateAmount = parseFloat($(this).find("div").html());
                    }
                });

                $("#" + rowId).find("td").each(function ()
                {
                    if ($(this).attr("field") == "CurrentAmount")
                    {
                        if (allocateAmount < 0)
                        {
                            $(this).find("input").val(allocateAmount);
                        }
                        else
                        {
                            if (leftAmount >= allocateAmount)
                            {
                                $(this).find("input").val(allocateAmount);
                            }
                            else
                            {
                                $(this).find("input").val(leftAmount);
                            }
                            leftAmount -= allocateAmount;
                        }
                    }
                });
            }
            return false;
        });
    });
</script>
